# missing values, type is "none","one","two","three","range","range+1","high","high+1","low","low+1"
#fmt_name, vlabs,vvals,missings[[i]],var_names
cphsoc_spss_to_stata_misval <- function(fmt_name,vlabs,vvals,mis,var_name)
{
  recode_str <- ''
  modify_fmt_str <- ''
  if_condition <- ''
  range_lab_str <- ''
  if(mis$type != 'none')
  {
    # get spss missing values
    mvals <- mis$value
    
    # spss discrete missing values
    if(mis$type %in% c("one","two","three"))  
    {
      midx <- match(mvals,vvals,nomatch=0)
    }
    else 
    {
      if (mis$type=='range')
      { 
        midx <- (1:length(vvals))[(mvals[1]<=vvals & vvals<=mvals[2])]
        if_condition <- paste(mvals[1],'<=',var_name,' & ',var_name,'<=',mvals[2],sep='')
        range_lab_str <- paste(mvals[1],' to ',mvals[2],sep='')
      }
      else if (mis$type=='range+1')
      {
        midx <- (1:length(vvals))[(mvals[1]<=vvals & vvals<=mvals[2]) | vvals==mvals[3]]
        if_condition <- paste(mvals[1],'<=',var_name,' & ',var_name,'<=',mvals[2],sep='')
        range_lab_str <- paste(mvals[1],'<= x <=',mvals[2],sep='')
      }
      else if (mis$type=='low')
      {
        midx <- (1:length(vvals))[(vvals<=mvals[1])]
        if_condition <- paste(var_name,'<=',mvals[1],sep='')
        range_lab_str <- paste('<=',mvals[1],sep='')
      }
      else if (mis$type=='low+1')
      {
        midx <- (1:length(vvals))[(vvals<=mvals[1] | vvals==mvals[2])]
        if_condition <- paste(var_name,'<=',mvals[1],' | ',var_name,'==',mvals[2],sep='')
        range_lab_str <- paste('<=',mvals[1],sep='')
      }
      else if (mis$type=='high')
      {
        midx <- (1:length(vvals))[(mvals[1]<=vvals)]
        if_condition <- paste(mvals[1],'<=',var_name,sep='')
        range_lab_str <- paste(mvals[1],'+',sep='')
      }
      else if (mis$type=='high+1')
      {
        midx <- (1:length(vvals))[(mvals[1]<=vvals | vvals==mvals[2])]
        if_condition <- paste(mvals[1],'<=',var_name,' | ',var_name,'==',mvals[2],sep='')
        range_lab_str <- paste(mvals[1],'+',sep='')
      }
      else {stop('This should not happen, spss_to_stata_missing_values()')}
      # create stata missing value for missing with undefined labels
    }
    # create stata missing values for defined value labels
    if(length(midx)>0)
    {
      # stata missing indicator (as many as there are known missing values)
      stmis <- paste('.',letters[1:length(midx)],sep='')

      # stata code to modify format
      for(m in 1:length(midx))
      {
        recode_str <- paste(recode_str,'* mvdecode ',var_name,', mv(',vvals[midx[m]],'=',stmis[m],')\n',sep='')
        modify_fmt_str <- paste(modify_fmt_str,'* label define ',fmt_name,' ',stmis[m],' "',vlabs[midx[m]],'", modify\n',sep='')
      }
    }
    if(nchar(if_condition)>0)
    {
      stelse <- paste('.',letters[(length(midx)+1)],sep='')
      recode_str <- paste(recode_str,'* replace ',var_name,'=',stelse,' if ',if_condition,'\n',sep='')
      # modify format
      modify_fmt_str <- paste(modify_fmt_str,'* label define ',fmt_name,' ',stelse,' "',range_lab_str,'", modify\n',sep='')
    }
  }
  list(val2mis_str=modify_fmt_str,vlabs_str=recode_str)
}
